import json
import requests
import pandas as pd
from os import listdir


def get_infos(s_v, a_v):
    Map = {}
    for year in ['2018', '2019', '2020']:
        List = []
        for t in range(1, 4):
            for p in range(1, 100):
                url = f'https://static-data.eol.cn/www/2.0/schoolspecialindex/{year}/{s_v}/{a_v}/{t}/{p}.json'
                response = requests.get(url)
                try:
                    data = json.loads(response.text)
                except:
                    return get_infos(s_v, a_v)
                if len(data) > 0:
                    List += data['data']['item']
                else:
                    break

        if len(List) != 0:
            data = pd.DataFrame(List)
            dic = {}
            type_1 = []
            for i in range(len(data.loc[data['type'] == '1'])):
                d = data.loc[data['type'] == '1'].iloc[i]
                type_1.append(d.to_dict())
            dic['1'] = type_1
            type_2 = []
            for i in range(len(data.loc[data['type'] == '2'])):
                d = data.loc[data['type'] == '2'].iloc[i]
                type_2.append(d.to_dict())
            dic['2'] = type_2
            type_3 = []
            for i in range(len(data.loc[data['type'] == '3'])):
                d = data.loc[data['type'] == '3'].iloc[i]
                type_3.append(d.to_dict())
            dic['3'] = type_3
            Map[year] = dic
    if len(Map) > 0:
        return Map
    else:
        return 0


if __name__ == '__main__':
    with open('./分组/1.json', 'r', encoding='utf-8') as f:
        school_id = f.read()
        school_id = json.loads(school_id)
    with open('地区_id.json', 'r', encoding='utf-8') as f:
        area_id = f.read()
        area_id = json.loads(area_id)
    for s_k, s_v in school_id.items():
        Map = {}
        if f'{s_k}.json' not in listdir('F:/高校分析与推荐系统/data/高校专业分数线'):
            dic = {}
            for a_k, a_v in area_id.items():
                d = get_infos(s_v, a_v)
                if d != 0:
                    dic[a_k] = d
                print(f'{s_k}  {a_k}   finish')
            Map[s_k] = dic
            Map['school_name'] = s_k
            with open(f'F:/高校分析与推荐系统/data/高校专业分数线/{s_k}.json', 'w', encoding='utf-8') as f:
                json.dump(Map, f, indent=4, ensure_ascii=False)


